package com.jjwxc.jwjskandriod.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.CountDownTimer;
import android.widget.EditText;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.widget.EnterPasswordDialog;

public class AppealActivity extends BaseActivity {

    private EditText ed_phone;
    private EditText ed_code;
    private TextView tv_code;
    private TextView tv_ok;

    @Override
    public int getContentViewId() {
        return R.layout.activity_appeal;
    }

    @Override
    public void findView() {
        setTitle("短信验证密码申诉");
        ed_phone = findViewById(R.id.ed_phone);
        ed_code = findViewById(R.id.ed_code);
        tv_code = findViewById(R.id.tv_code);
        tv_ok = findViewById(R.id.tv_ok);
    }

    @Override
    public void setListener() {
        tv_ok.setOnClickListener(v -> {
            String phone = ed_phone.getText().toString();
            String code = ed_code.getText().toString();

            if (StUtils.isEmpty(phone)) {
                showToast("请输入手机号");
                return;
            }
            if (StUtils.isEmpty(code)) {
                showToast("请输入验证码");
                return;
            }
            String p = PreUtils.getString("phone", "");
            if (!p.equals(phone)) {
                showToast("非本账号的手机，无法通过验证");
                return;
            }
            new EnterPasswordDialog(AppealActivity.this, new EnterPasswordDialog.DialogClickListener() {
                @Override
                public void onOkClick(Dialog dialog) {
                    Intent intent = new Intent(AppealActivity.this, AdolescentActivity.class);
                    startActivity(intent);
                }

                @Override
                public void onNoClick(Dialog dialog) {

                }
            }).show();

        });

        tv_code.setOnClickListener(v -> {
            String phone = ed_phone.getText().toString();
            if (StUtils.isEmpty(phone)) {
                showToast("请输入手机号");
                return;
            }
            String p = PreUtils.getString("phone", "");
            if (!StUtils.isEmpty(p) && p.equals(phone)) {
                sendSms(phone);
            } else {
                showToast("非本账号的手机，无法通过验证");
            }
        });
    }

    @Override
    public void afterCreate() {

    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tv_code.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            tv_code.setEnabled(true);
            tv_code.setText("发送验证码");
        }
    };

    public void sendSms(String phone) {
        post(Url.sendSms, null, String.class, new FFNetWorkCallBack<String>() {
            @Override
            public void onSuccess(String response) {
                tv_code.setEnabled(false);
                timer.start();
            }

            @Override
            public boolean onFail(String response) {
                showToast("出错，请联系服务开发人员");
                return false;
            }
        }, "{\"mobile\":\"" + phone + "\"}");
    }
}
